home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / tools / nwtp06 / shwsaps.pas < prev    next >
Pascal/Delphi Source File  |  1996-07-10  |  5KB  |  190 lines

  1. {$X+,V-,B-}
  2. program ShSAPs;
  3.  
  4. { Testprogram for the nwSAP unit / NwTP 0.6 (c) 1993,1995 R.Spronk }
  5.  
  6. { Dump all incoming SAP broadcasts on screen;
  7.   Sends -no- packets; receiving packets only }
  8.  
  9. { Demonstrates
  10.   -The use of the Service Advertizing Protocol;
  11.   -Asynchronous handling of receiving and processing
  12.    (using 1 receive ESR and intermediate buffers   }
  13.  
  14. uses crt,nwMisc,nwIPX,nwSAP;
  15.  
  16. CONST SAPsocket=$0452;
  17.       BUFSIZ=511;
  18.       { May 'hang' your WS if more than 70 SAP broadcast were received
  19.         in a short interval (a few ticks). Increase the BUFSIZ value. }
  20.  
  21. Type TSAPserver=record
  22.                 ObjType:word;
  23.                 Name   :array[1..48] of byte; { asciiz }
  24.                 Address:TinternetworkAddress;
  25.                 Hops   :word;
  26.                 end;
  27.  
  28.      TSAPresponse=record
  29.                   ResponseType:word; { 0002 General server; 0004 nearest server }
  30.                   ServerEntry:array[1..7] of TSAPserver;
  31.                   end;
  32.  
  33. Type String48=string[48];
  34.      Tservices=record
  35.                InUseFlag  :Byte; { 0: not being accessed by other threads }
  36.                TimeStamp :Word; { Ticks / max 60. minutes }
  37.                ObjType:word;
  38.                Name   :array[1..48] of byte; { asciiz }
  39.                Address:TinternetworkAddress;
  40.                Hops   :word;
  41.                end;
  42.  
  43. Var ServBuf:array[0..BUFSIZ] of TServices;
  44.     ECBServBufInd:word; { 0..BUFSIZ }
  45.     ServBufInd   :word; { 0..BUFSIZ }
  46.  
  47.     StartTicks:Longint;
  48.  
  49.     PktCount:word;
  50.  
  51. Var ReceiveEcb    :Tecb;
  52.     IpxHdr        :TipxHeader;
  53.     socket        :word;
  54.     IPXreceiveBuffer: array[1..546] of byte;
  55.     SAPreceiveBuffer: TSAPresponse absolute IPXreceiveBuffer;
  56.  
  57.     ReceivedBufLen:word;
  58.     PacketReceived:boolean;
  59.  
  60.     RecString     :string;
  61.  
  62.     NewStack:array[1..1024] of word;  { !! used by ESR }
  63.     StackBottom:word;                 { !! used by ESR }
  64.     ESRctr:byte;                      { !! used by SAP ESR }
  65.  
  66.  
  67. {$F+}
  68. Procedure SAPListenESRhandler(Var p:Tpecb);
  69. begin
  70. if SAPreceiveBuffer.Responsetype=$0200 { 0002 hi-lo: general server SAP reply }
  71.  then begin
  72.       ESRctr:=1;
  73.       while (ESRctr<=7) and (SAPreceiveBuffer.ServerEntry[ESRctr].ObjType>$0000)
  74.        do begin
  75.           while ServBuf[ECBservBufInd].inUseFlag>0
  76.            do begin
  77.               inc(ECBServBufInd);
  78.               ECBservBufInd:=ECBservBufInd and BUFSIZ;
  79.               end;
  80.           with SAPreceiveBuffer.ServerEntry[ESRctr]
  81.            do begin
  82.               Move(ObjType,ServBuf[ECBServBufInd].ObjType,SizeOf(TSAPserver));
  83.               ObjType:=$0000; { To mark that the entry has been dealt with;
  84.                                 to 'clear' receive buffer }
  85.               end;
  86.           with ServBuf[ECBServBufInd]
  87.            do begin
  88.               IPXgetIntervalMarker(TimeStamp);
  89.               InUseFlag:=$FF;
  90.               end;
  91.           inc(ESRctr);
  92.           end;
  93.       PacketReceived:=true;
  94.       inc(PktCount);
  95.       end;
  96. IPXListenForPacket(ReceiveECB);
  97. end;
  98. {$F-}
  99.  
  100. {$F+}
  101. Procedure SAPListenESR; assembler;
  102. asm { ES:SI are the only valid registers when entering this procedure ! }
  103.     mov dx, seg stackbottom
  104.     mov ds, dx
  105.  
  106.     mov dx,ss  { setup of a new local stack }
  107.     mov bx,sp  { ss:sp copied to dx:bx}
  108.     mov ax,ds
  109.     mov ss,ax
  110.     mov sp,offset stackbottom
  111.     push dx    { push old ss:sp on new stack }
  112.     push bx
  113.  
  114.     push es    { push es:si on stack as local vars }
  115.     push si
  116.     mov  di,sp
  117.  
  118.     push ss    { push address of local ptr on stack }
  119.     push di
  120.     CALL SAPListenEsrHandler
  121.  
  122.     add sp,4   { skip stack ptr-copy }
  123.     pop bx     { restore ss:sp from new stack }
  124.     pop dx
  125.     mov sp,bx
  126.     mov ss,dx
  127. end;
  128. {$F-}
  129.  
  130.  
  131. Var ServerName:string;
  132.  
  133. begin
  134. IF NOT IpxInitialize
  135.  then begin
  136.       writeln('Ipx needs to be installed.');
  137.       halt(1);
  138.       end;
  139. socket:=SAPSocket;
  140. IF NOT IPXopenSocket(Socket,SHORT_LIVED_SOCKET)
  141.  then begin
  142.       writeln('IPXopenSocket returned error# ',nwIPX.result);
  143.       halt(1);
  144.       end;
  145.  
  146. PktCount:=0;
  147. ECBservBufInd:=0;
  148. PacketReceived:=False;
  149. { Empty receive buffer (ReceiveEcb.fragment[2].address^) }
  150. FillChar(IPXreceiveBuffer,546,#0);
  151.  
  152. { Setup ECB and IPX header }
  153. IPXsetupListenECB(Addr(SAPListenESR),SAPsocket,@IPXreceiveBuffer,546,
  154.                   IpxHdr,ReceiveEcb);
  155.  
  156. IPXListenForPacket(ReceiveECB);
  157.  
  158. ServBufInd:=0;
  159. REPEAT
  160.  
  161.  WHILE (ServBufInd<512) and (NOT keypressed)
  162.   do begin
  163.      IPXrelinquishControl;
  164.  
  165.      IF ServBuf[ServBufInd].InUseFlag>0
  166.       then begin
  167.            with ServBuf[ServBufInd]
  168.             do begin
  169.                writeln('---------');
  170.                writeln('BufIndex:',ServBufInd);
  171.                writeln('Timestamp: ',HexStr(TimeStamp,4));
  172.                writeln('ObjType  : ',HexStr(swap(ObjType),4));
  173.                ZStrCopy(ServerName,name[1],48);
  174.                writeln('ServerNm : ',ServerName);
  175.                writeln('Address  : ',HexDumpStr(Address,24));
  176.                writeln('Hops     : ',HexStr(swap(Hops),4));
  177.                end;
  178.            ServBuf[ServBufInd].InUseFlag:=0;
  179.            end;
  180.  
  181.      inc(ServBufInd);ServBufInd:=ServBufInd and BUFSIZ;
  182.      end;
  183.  
  184. UNTIL KeyPressed;
  185.  
  186. IF NOT IPXcloseSocket(SAPsocket)
  187.  then writeln('IPXcloseSocket returned error# ',nwIPX.result);
  188.  
  189. end.
  190.